<?xml version="1.0" encoding="windows-1251"?>
<!--CodeRegMask="nwz|cho"-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="MiniEditorCommon.xslt"/>

  <xsl:template name="Contours">
    <xsl:param name="Cs_Id"/>
    <Contours>
      <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Square']/*">
        <xsl:variable name="Contour">
          <xsl:variable name="Contour0">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'Contour'"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="substring-before(substring-after($Contour0,'('),')')"/>
        </xsl:variable>
        <xsl:variable name="Area">
          <xsl:call-template name="Value">
            <xsl:with-param name="x" select="'Area'"/>
          </xsl:call-template>
        </xsl:variable>
        <Contour>
          <xsl:variable name="Definition">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'Definition'"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="Number_PP">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'Number_PP'"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:if test="$Definition!=''">
            <xsl:attribute name="Definition">
              <xsl:value-of select="$Definition"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="$Number_PP!=''">
            <xsl:attribute name="Number_PP">
              <xsl:value-of select="$Number_PP"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:choose>
            <xsl:when test="$VersionMP='02'">
              <xsl:for-each select="../../../Page[@Code='Areas']/*[@Code='Area' and @Selected='true']/*">
                <Area>
                  <Area>
                    <xsl:value-of select="$Area"/>
                  </Area>
                  <Unit>
                    <xsl:call-template name="Value">
                      <xsl:with-param name="x" select="'Unit'"/>
                    </xsl:call-template>
                  </Unit>
                  <Innccuracy>
                    <xsl:call-template name="Value">
                      <xsl:with-param name="x" select="'Innccuracy'"/>
                    </xsl:call-template>
                  </Innccuracy>
                </Area>
              </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
              <Areas>
                <xsl:for-each select="../../../Page[@Code='Areas']/*[@Code='Area' and @Selected='true']/*">
                  <Area>
                    <AreaCode>
                      <xsl:call-template name="Value">
                        <xsl:with-param name="x" select="'AreaCode'"/>
                      </xsl:call-template>
                    </AreaCode>
                    <Area>
                      <xsl:value-of select="$Area"/>
                    </Area>
                    <Unit>
                      <xsl:call-template name="Value">
                        <xsl:with-param name="x" select="'Unit'"/>
                      </xsl:call-template>
                    </Unit>
                    <Innccuracy>
                      <xsl:call-template name="Value">
                        <xsl:with-param name="x" select="'Innccuracy'"/>
                      </xsl:call-template>
                    </Innccuracy>
                  </Area>
                </xsl:for-each>
              </Areas>
            </xsl:otherwise>
          </xsl:choose>
          
          <xsl:for-each select="../../../.">
            <xsl:call-template name="Entity_Spatial">
              <xsl:with-param name="Cs_Id" select="$Cs_Id"/>
              <xsl:with-param name="Contour" select="$Contour"/>
            </xsl:call-template>
          </xsl:for-each>
        </Contour>
      </xsl:for-each>
    </Contours>
  </xsl:template>

  <xsl:template name="Entity_Spatial">
    <xsl:param name="Cs_Id"/>
    <xsl:param name="Contour"/>
    <Entity_Spatial>
      <xsl:attribute name="Map_Id"/>
      <xsl:attribute name="Sp_Obj_Id"/>
      <xsl:attribute name="Ent_Sys">
        <xsl:value-of select="$Cs_Id"/>
      </xsl:attribute>
      <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
        <xsl:for-each select="*">
          <xsl:variable name="pos">
            <xsl:value-of select="position()"/>
          </xsl:variable>
          <xsl:variable name="Spat">
            <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
          </xsl:variable>
          <xsl:variable name="Spatial">
            <xsl:choose>
              <xsl:when test="$Spat=''">
                <xsl:value-of select="'1'"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$Spat"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:if test="($Contour='') or ($Contour = $Spatial) or ($Contour = substring-before($Spatial,'.'))">
            <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
              <xsl:call-template name="Spatial_Element">
                <xsl:with-param name="Spatial" select="$Spatial"/>
              </xsl:call-template>
            </xsl:if>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:if test="Page[@Code='Entity_Spatial']/*[@Code='Edge']">
        <xsl:call-template name="Borders">
          <xsl:with-param name="Contour" select="$Contour"/>
        </xsl:call-template>
      </xsl:if>
    </Entity_Spatial>
  </xsl:template>

  <xsl:template name="Spatial_Element">
    <xsl:param name="Spatial"/>
    <Spatial_Element>
      <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']/*">
        <xsl:variable name="TestSUO0">
          <xsl:variable name="TestSUO00">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'Num_Geopoint'"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="translate($TestSUO00,' ','')"/>
        </xsl:variable>
        <xsl:variable name="SpatialR">
          <xsl:choose>
            <xsl:when test="contains($TestSUO0,'(')">
              <xsl:value-of select="substring-before(substring-after($TestSUO0,'('),')')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="1"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="X">
          <xsl:call-template name="Value">
            <xsl:with-param name="x" select="'X'"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="Y">
          <xsl:call-template name="Value">
            <xsl:with-param name="x" select="'Y'"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:if test="$Spatial=$SpatialR and $X!='л' and $Y!='л'">
          <Spelement_Unit Type_Unit="Точка">
            <xsl:attribute name="Su_Nmb">
              <xsl:value-of select="position()"/>
            </xsl:attribute>
            <Ordinate>
              <xsl:variable name="TestSUO1">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Geopoint_Zacrep'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUO1!=''">
                <xsl:attribute name="Geopoint_Zacrep">
                  <xsl:value-of select="$TestSUO1"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:variable name="TestSUO2">
                <xsl:choose>
                  <xsl:when test="contains($TestSUO0,'(')">
                    <xsl:value-of select="substring-before($TestSUO0,'(')"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$TestSUO0"/>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:if test="$TestSUO2!=''">
                <xsl:attribute name="Num_Geopoint">
                  <xsl:value-of select="$TestSUO2"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:variable name="TestSUO3">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Point_Pref'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUO3!=''">
                <xsl:attribute name="Point_Pref">
                  <xsl:value-of select="$TestSUO3"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:attribute name="Ord_Nmb">
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:attribute name="X">
                <xsl:value-of select="$X"/>
              </xsl:attribute>
              <xsl:attribute name="Y">
                <xsl:value-of select="$Y"/>
              </xsl:attribute>
              <xsl:variable name="TestSUO4">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Delta_Geopoint'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUO4!=''">
                <xsl:attribute name="Delta_Geopoint">
                  <xsl:value-of select="$TestSUO4"/>
                </xsl:attribute>
              </xsl:if>
            </Ordinate>
          </Spelement_Unit>
        </xsl:if>
      </xsl:for-each>
    </Spatial_Element>
  </xsl:template>

  <xsl:template name="Borders">
    <xsl:param name="Contour"/>
    <Borders>
      <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Edge']/*">
        <xsl:variable name="TestSUE0">
          <xsl:variable name="TestSUE00">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'FromTo'"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="translate($TestSUE00,' ','')"/>
        </xsl:variable>
        <xsl:variable name="Spat">
          <xsl:value-of select="substring-before(substring-after($TestSUE0,'('),')')"/>
        </xsl:variable>
        <xsl:variable name="Spatial">
          <xsl:choose>
            <xsl:when test="$Spat=''">
              <xsl:value-of select="'1'"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$Spat"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:if test="($Contour='') or ($Contour = $Spatial) or ($Contour = substring-before($Spatial,'.'))">
          <Border>
            <xsl:variable name="SpatialNumber">
              <xsl:choose>
                <xsl:when test="contains($Spatial,'.')">
                  <xsl:value-of select="substring-after($Spatial,'.') + 1"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="1"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:attribute name="Point1">
              <xsl:value-of select="substring-before($TestSUE0,'-')"/>
            </xsl:attribute>
            <xsl:attribute name="Point2">
              <xsl:choose>
                <xsl:when test="contains($TestSUE0,'(')">
                  <xsl:value-of select="substring-before(substring-after($TestSUE0,'-'),'(')"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="substring-after($TestSUE0,'-')"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:attribute name="Spatial">
              <xsl:value-of select="$SpatialNumber"/>
            </xsl:attribute>
            <Edge>
              <xsl:variable name="TestSUE1">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Neighbours'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUE1!=''">
                <Neighbours>
                  <xsl:for-each select="*[@Code='Neighbours']/*">
                    <CadastralNumber>
                      <xsl:value-of select="."/>
                    </CadastralNumber>
                  </xsl:for-each>
                </Neighbours>
              </xsl:if>
              <xsl:variable name="TestSUE2">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Length'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUE2!=''">
                <Length>
                  <xsl:value-of select="$TestSUE2"/>
                </Length>
              </xsl:if>
              <xsl:variable name="TestSUE3">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Direction_Angle'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUE3!=''">
                <Direction_Angle>
                  <Degree>
                    <xsl:value-of select="substring-before($TestSUE3,'.')"/>
                  </Degree>
                  <Minute>
                    <xsl:value-of select="substring-after($TestSUE3,'.')"/>
                  </Minute>
                </Direction_Angle>
              </xsl:if>
              <xsl:variable name="TestSUE4">
                <xsl:call-template name="Value">
                  <xsl:with-param name="x" select="'Definition'"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$TestSUE4!=''">
                <Definition>
                  <xsl:value-of select="$TestSUE4"/>
                </Definition>
              </xsl:if>
            </Edge>
          </Border>
        </xsl:if>
      </xsl:for-each>
    </Borders>
  </xsl:template>
</xsl:stylesheet>
